﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.IO;
using System.Data;
using System.Configuration;
using Newtonsoft.Json;

namespace PromotionEngine
{
    public partial class Import : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static string GetTemplate(string dataType)
        {
            try
            {
                if (dataType.ToUpper() == "CUSTOMER")
                {
                    return JsonConvert.SerializeObject(Utils.Utils.TEMPLATE_PATH + Utils.Utils.IMPORT_CUSTOMER);
                }
                else if (dataType.ToUpper() == "ITEM")
                {
                    return JsonConvert.SerializeObject(Utils.Utils.TEMPLATE_PATH + Utils.Utils.IMPORT_ITEM);
                }
                else if (dataType.ToUpper() == "WAREHOUSE")
                {
                    return JsonConvert.SerializeObject(Utils.Utils.TEMPLATE_PATH + Utils.Utils.IMPORT_WAREHOUSE);
                }
                else if (dataType.ToUpper() == "SALESPERSON")
                {
                    return JsonConvert.SerializeObject(Utils.Utils.TEMPLATE_PATH + Utils.Utils.IMPORT_SALESPERSON);
                }
            }
            catch (Exception ex)
            {

            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static string Save(string dataType)
        {
            try
            {
                string fileLocation = HttpContext.Current.Server.MapPath(Utils.Utils.IMPORT_PATH);
                string fileName = Utils.Utils.IMPORT_FILENAME;

                DataTable tbCsv = new DataTable();
                if (File.Exists(fileLocation + fileName))
                {
                    tbCsv = Utils.Utils.ImportCSVFile(fileName, fileLocation);
                    Promotion pmt = new Promotion();
                    string createdUserID = string.Empty, companyCode = string.Empty;
                    if (HttpContext.Current.Session["LoginUser"] != null)
                    {
                        DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                        if (tbUser != null && tbUser.Rows.Count > 0)
                        {
                            createdUserID = tbUser.Rows[0]["UserID"].ToString();
                            companyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                        }
                    }

                    if (dataType.ToUpper() == "CUSTOMER")
                    {
                        foreach (DataRow r in tbCsv.Rows)
                        {
                            DateTime birthday = DateTime.Now.Date;
                            DateTime specialDay = DateTime.Now.Date;
                            if (r["Birthday"].ToString().Length > 0 && Utils.Utils.isDateTime(r["Birthday"].ToString()))
                            {
                                birthday = DateTime.Parse(r["Birthday"].ToString());
                            }
                            if (r["SpecialDay"].ToString().Length > 0 && Utils.Utils.isDateTime(r["SpecialDay"].ToString()))
                            {
                                specialDay = DateTime.Parse(r["SpecialDay"].ToString());
                            }
                            pmt.UpdateJsonCustomer("", r["CustomerCode"].ToString(), r["CustomerName"].ToString(), companyCode, createdUserID, DateTime.Now.Date
                                , true, r["BillingAddress"].ToString(), r["ShippingAddress"].ToString(), r["ContactPerson"].ToString(), r["PhoneNumber"].ToString(), r["Email"].ToString()
                                , birthday, specialDay, "0", ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), true);
                        }
                    }
                    else if (dataType.ToUpper() == "ITEM")
                    {
                        foreach (DataRow r in tbCsv.Rows)
                        {
                            pmt.UpdateJsonItem("", r["ItemCode"].ToString(), r["ItemName"].ToString(), companyCode, double.Parse(r["Price"].ToString()), createdUserID, DateTime.Now.Date, true, true, ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), true);
                        }
                    }
                    else if (dataType.ToUpper() == "WAREHOUSE")
                    {
                        foreach (DataRow r in tbCsv.Rows)
                        {
                            pmt.UpdateJsonWarehouse("", r["WarehouseCode"].ToString(), r["WarehouseName"].ToString(), companyCode, r["Address"].ToString(), createdUserID, DateTime.Now.Date, true, ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), true);
                        }
                    }
                    else if (dataType.ToUpper() == "SALESPERSON")
                    {
                        foreach (DataRow r in tbCsv.Rows)
                        {
                            pmt.UpdateJsonSalesPerson("", r["SalesPersonCode"].ToString(), r["SalesPersonName"].ToString(), companyCode, createdUserID, "", ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return string.Empty;
        }
    }
}